//==============================================================================
//
//               Copyright (C) 2004  Dick van Oudheusden
//  
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
//==============================================================================
// 
//  $Date: 2004-06-22 11:52:05 $ $Revision: 1.5 $
//
//==============================================================================

#include <stdlib.h>

#include "ofc/config.h"

#include "ofc/DDirectory.h"

#include "DInc.h"
#include "DTest.h"

//-Misc------------------------------------------------------------------------

void DDirectory_test()
{
  DDirectory    *dir    = [DDirectory new];
  DDirectory    *cwd    = nil;
  DText         *text   = nil;
  DList         *list   = nil;
  DDirectory    *temp   = nil;  
  
  STARTTEST();

  // Class methods
  cwd = [DDirectory current];

  TEST(cwd != nil);
  
  // printf("Current Directory: %s\n", [cwd path]);

  TEST([DDirectory exist :[cwd path]]);
  
  list = [DDirectory childs :[cwd path] :nil];
  
  TEST(list != nil);
  
  TEST([list length] > 40);
  // printf("Items in directory: %ld\n", [list length]);
  
  text = [list get :10];
  
  TEST(text != nil);
  
  // printf("10th item: %s\n", [text cstring]);

  [list free];

#ifdef WIN32
  // test absolute path  
  [dir path :"c:\\MinGW\\"];
  TEST(strcmp([dir path], "c:\\MinGW") == 0);
  TEST([dir isAbsolute]);
  list = [dir names];
  TEST([list length] == 2);
  [dir names :list];
  TEST(strcmp([dir path], "c:\\MinGW") == 0);
  TEST(strcmp([dir name], "MinGW") == 0);
  TEST([dir drive] == 'c');
  [list free];

  [dir path :"\\temp"]; 
  TEST(strcmp([dir path], "\\temp") == 0);
  TEST([dir isAbsolute]);
  list = [dir names];
  TEST([list length] == 2);
  [dir names :list];
  TEST(strcmp([dir path], "\\temp") == 0);
  TEST(strcmp([dir name], "temp") == 0);
  TEST([dir drive] == EOS);
  [list free];

  // test relative path
  [dir path :"a/"];
  TEST(strcmp([dir path], "a") == 0);
  TEST(![dir isAbsolute]);
  TEST(strcmp([dir name], "a") == 0);
  list = [dir names];
  TEST([list length] == 1);
  [dir names :list];
  [list free];
  TEST(strcmp([dir path], "a") == 0);
  [dir child :"/extra"];
  TEST(strcmp([dir path], "a\\extra") == 0);

  [dir path :"c:MinGW"];
  TEST(strcmp([dir path], "c:MinGW") == 0);
  TEST(![dir isAbsolute]);
  list = [dir names];
  TEST([list length] == 2);
  [dir names :list];
  TEST(strcmp([dir path], "c:MinGW") == 0); 
  TEST(strcmp([dir name], "MinGW") == 0);
  TEST([dir drive] == 'c');
  [dir child :"bin"];
  TEST(strcmp([dir path],"c:MinGW\\bin") == 0);
  [list free];

  [dir path :"//"];
  TEST(strcmp([dir path], "//") == 0);
  list = [dir names]; 
  [dir names :list];
  TEST(strcmp([dir path], "//") == 0);
  [list free];
  
  TEST([dir name] == NULL);
  TEST(![dir parent]);
#else
  // test absolute path
  [dir path :"/home/user/bin/"];
  TEST(strcmp([dir path], "/home/user/bin") == 0);
  TEST([dir isAbsolute]);
  
  TEST(strcmp([dir name], "bin") == 0);
  
  [dir child :"exe"];
  TEST(strcmp([dir path], "/home/user/bin/exe") == 0);
  
  list = [dir names];
  TEST([list length] == 5);
  [dir names :list];
  [list free];
  TEST(strcmp([dir path], "/home/user/bin/exe") == 0);
  
  TEST([dir parent]);
  TEST(strcmp([dir path], "/home/user/bin") == 0);

  // test relative path
  [dir path :"a/"];
  TEST(strcmp([dir path], "a") == 0);
  TEST(![dir isAbsolute]);
  
  TEST(strcmp([dir name], "a") == 0);
  
  list = [dir names];
  TEST([list length] == 1);
  [dir names :list];
  [list free];
  TEST(strcmp([dir path], "a") == 0);
  
  [dir child :"/extra"];
  TEST(strcmp([dir path], "a/extra") == 0);


  // test only root
  [dir path :"/"];
  TEST(strcmp([dir path], "/") == 0);

  TEST([dir name] == NULL);

  list = [dir names];
  TEST([list length] == 1);
  [dir names :list];
  [list free];
  TEST(strcmp([dir path], "/") == 0);
 
  TEST(![dir parent]);
  [dir child :"font/"];
  TEST(strcmp([dir path], "/font") == 0);


  // test small path
  [dir path :"/home"];
  TEST(strcmp([dir path], "/home") == 0);
  
  TEST(strcmp([dir name], "home") == 0);
  TEST([dir parent]);
  TEST(strcmp([dir path], "/") == 0);

  // test path with ..
  [dir path :"/home/user/../"];
  TEST(strcmp([dir path], "/home/user/..") == 0);

  list = [dir names];
  TEST([list length] == 4);
  [dir names :list];
  [list free];
  TEST(strcmp([dir path], "/home/user/..") == 0);
  
  TEST(strcmp([dir name], "..") == 0);
  TEST([dir parent]);
  TEST(strcmp([dir path], "/home/user") == 0);

  
  [dir path :"a/"];
  TEST([dir parent]);
  TEST([dir isEmpty]);
#endif
        
  temp = [DDirectory temp];
  
  TEST(temp != nil);
  
  if (temp != nil)
  {
    DDirectory *cpy = nil;

    [dir path  :[temp path]];
    
    [dir child :"ofc"];
    
    TEST([DDirectory create :[dir path]]);
    
    TEST([DDirectory exist  :[dir path]]);
    
    cpy = [dir copy];

    TEST([cpy parent]);
    
    [cpy child :"ffc"];

    TEST(![DDirectory exist :[cpy path]]);
    
    TEST([DDirectory move :[dir path] :[cpy path]]);
    
    TEST(![DDirectory exist  :[dir path]]);
    TEST([DDirectory exist :[cpy path]]);
    
    TEST([DDirectory remove :[cpy path]]);
    
    TEST(![DDirectory exist :[cpy path]]);

    [cpy free];
    [temp free];
  }
  
//  TEST([tok nextToken] == DTK_STRING);
  
  [dir free];
    
  STOPTEST();
}

